package com.etalk.crm.controller;

import com.alibaba.fastjson.JSONObject;
import com.etalk.crm.pojo.ActivityInfo;
import com.etalk.crm.pojo.KcPackage;
import com.etalk.crm.service.ActivityInfoService;
import com.etalk.crm.utils.OssGetObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.etalk.crm.utils.OssGetObject.deleteActivityInfoAttachment;

/**
 * @Auther: James
 * @Date: 2019/2/26 15:16
 * @Description:
 */
@Controller
@RequestMapping(value = "/activityInfoCtl", produces = "application/json; charset=utf-8")
public class ActivityInfoCtl {
    /**
     * 动态附件保存路径
     */
    public static final String ACTIVITY_ATTACHMENT_UPLOAD_PATH = "crm/activityInfo" + "/";

    @Value("https://hnres.etalk365.com/")
    String fileUrl;

    @Resource
    private ActivityInfoService activityInfoService;


    /*
     * @Author James
     * @Description 活动列表
     * @Date 18:45 2019/2/26
     * @Param
     * @return
     **/
    @RequestMapping(value = "/activityInfoList")
    public String activityInfo(Model model, Integer pageNum, Integer pageSize, String state, String searchKey){
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        if(state != ""){
            model.addAttribute("state",state);
        }
        if(searchKey != ""){
            model.addAttribute("searchKey",searchKey);
        }
        PageInfo<ActivityInfo> pageInfo = activityInfoService.activityInfoList(pageNum,pageSize,state,searchKey);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("pageSize",pageSize);
        return "activityInfo/activity_info_list";
    }

    /*
     * @Author James
     * @Description 活动添加页面
     * @Date 18:45 2019/2/26
     * @Param
     * @return
     **/
    @RequestMapping(value = "/toAddActivityInfo")
    public String toAddactivityInfo(Model model){
        //促销 demo套餐列表。
        List<KcPackage> packageList = activityInfoService.selectPackage();
        model.addAttribute("packageList", packageList);
        //运营活动列表
        List<ActivityInfo> promotionList = activityInfoService.promotionList();
        model.addAttribute("promotionList", promotionList);
        return "activityInfo/add_activity_info";
    }

    /*
     * @Author James
     * @Description 活动编辑页面
     * @Date 18:45 2019/2/26
     * @Param  id 活动id
     * @return
     **/
    @RequestMapping(value = "/toEidtActivityInfo")
    public String toEidtActivityInfo(Model model,int id){
        ActivityInfo activityInfo = activityInfoService.selectById(id);
        model.addAttribute("activityInfo", activityInfo);
         //促销 demo套餐列表
        List<KcPackage> packageList = activityInfoService.selectPackage();
        model.addAttribute("packageList", packageList);
        //运营活动列表
        List<ActivityInfo> promotionList = activityInfoService.promotionList();
        model.addAttribute("promotionList", promotionList);
        return "activityInfo/eidt_activity_info";
    }

     /*
     * @Author James
     * @Description 查看活动页面
     * @Date 18:45 2019/2/26
     * @Param id 活动id
     * @return
     **/
    @RequestMapping(value = "/toLookupActivityInfo")
    public String toLookupActivityInfo(Model model,int id){
        List<KcPackage> packageList = activityInfoService.selectPackage();
        ActivityInfo activityInfo = activityInfoService.selectById(id);
        model.addAttribute("packageList", packageList);
        model.addAttribute("activityInfo", activityInfo);
        return "activityInfo/lookup_activity_info";
    }

    /*
     * @Author James
     * @Description 删除活动
     * @Date 18:45 2019/2/26
     * @Param id 活动id
     * @return
     **/
    @RequestMapping(value = "/delActivityInfo",method = RequestMethod.POST)
    @ResponseBody
    public String delActivityInfo(Model model,int id){
        Map<String,Object> jsonMap = new HashMap<String,Object>();
        int result = activityInfoService.delActivityInfo(id);
        if(result > 0){
            jsonMap.put("code", 1);
            jsonMap.put("msg", "删除成功");
        }else{
            jsonMap.put("code", 0);
            jsonMap.put("msg", "删除失败");
        }
        return JSONObject.toJSONString(jsonMap);
    }

     /*
     * @Author James
     * @Description 删除活动
     * @Date 18:45 2019/2/26
     * @Param parameter：  ActivityInfo相关参数
     * @return
     **/
    @RequestMapping(value = "/addActivityInfo",method = RequestMethod.POST)
    @ResponseBody
    public String addActivityInfo(@RequestBody ActivityInfo parameter, HttpSession session){
        Map<String,Object> jsonMap = new HashMap<String,Object>();
        parameter.setAddUser(session.getAttribute("loginname").toString());
        int result = activityInfoService.addActivityInfo(parameter);
        if(result > 0){
            jsonMap.put("status", 1);
            jsonMap.put("msg", "添加成功");
        }else if(result == -1){
            jsonMap.put("status", -1);
            jsonMap.put("msg", "该活动已存在");
        }else{
            jsonMap.put("status", 0);
            jsonMap.put("msg", "添加失败");
        }
        return JSONObject.toJSONString(jsonMap);
    }

    /*
     * @Author James
     * @Description 删除活动
     * @Date 18:45 2019/2/26
     * @Param parameter：  ActivityInfo相关参数
     * @return
     **/
    @RequestMapping(value = "/editActivityInfo",method = RequestMethod.POST)
    @ResponseBody
    public String editActivityInfo(@RequestBody ActivityInfo parameter, HttpSession session){
        Map<String,Object> jsonMap = new HashMap<String,Object>();
        int result = activityInfoService.updateActivityInfo(parameter);
        if(result > 0){
            jsonMap.put("code", 1);
            jsonMap.put("msg", "修改成功");
        }else{
            jsonMap.put("code", 0);
            jsonMap.put("msg", "修改失败");
        }
        return JSONObject.toJSONString(jsonMap);
    }
    
     /*
      * @Author James
      * @Description 上传 oss 封面设置 转发路径
      * @Date 14:04 2019/2/27
      * @Param 
      * @return 
      **/
    @RequestMapping(value = "/uploadActivityInfoAttachment",method = RequestMethod.POST)
    @ResponseBody
    public String uploadActivityInfoAttachment(HttpServletRequest request,MultipartFile multipartfile) {
        Map<String,Object> jsonMap = new HashMap<>();
        MultipartFile file = ((MultipartHttpServletRequest)request).getFile("file");
        if (file != null) {
            String currentFileName = file.getOriginalFilename();
            String fileExt = currentFileName.substring(currentFileName.lastIndexOf(".") + 1).trim().toLowerCase();
            SimpleDateFormat sdfForFileName = new SimpleDateFormat("yyyyMMddHHmmss");
            String newName = sdfForFileName.format(new Date()) + new Random().nextInt(1000) + "." + fileExt;
            newName = currentFileName.replace("."+fileExt,"") + "_" + new Random().nextInt(1000) + "." + fileExt;
            System.out.println("new file name==>" + newName);
            try {
                OssGetObject.uploadActivityInfoAttachment(file, newName);

                jsonMap.put("imgUrl",fileUrl+ACTIVITY_ATTACHMENT_UPLOAD_PATH + newName);
                 jsonMap.put("img",ACTIVITY_ATTACHMENT_UPLOAD_PATH + newName);
            } catch (Exception e) {
                jsonMap.put("msg","上传失败");
            }
        }
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 删除oss  上的 图片
     * @param path
     * @return
     */
    @RequestMapping(value = "/delImg",method = RequestMethod.POST)
    @ResponseBody
    public String delImg(String path){
        Map<String,Object> jsonMap = new HashMap();
        deleteActivityInfoAttachment(path);
        jsonMap.put("code",1);
        jsonMap.put("msg","删除成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 活动管理
     * @param model
     * @param pageNum
     * @param pageSize
     * @param state 状态
     * @param searchKey  搜索字段
     * @return
     */
    @RequestMapping(value = "/promotionPage")
    public String promotionPage (Model model, Integer pageNum, Integer pageSize, String state, String searchKey){
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        if(state != ""){
            model.addAttribute("state",state);
        }
        if(searchKey != ""){
            model.addAttribute("searchKey",searchKey);
        }
        PageInfo<ActivityInfo> pageInfo = activityInfoService.promotionPage(pageNum,pageSize,state,searchKey);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("pageSize",pageSize);
        return "promotion/promotion_list";
    }


    /*
     * @Author James
     * @Description 活动添加
     * @Date 14:54 2019/2/28
     * @Param
     * @return
     **/
    @RequestMapping(value = "/toAddPromotion")
    public String toAddPromotion(){
        return "promotion/add_promotion";
    }


    /*
     * @Author James
     * @Description 删除活动
     * @Date 15:37 2019/2/28
     * @Param
     * @return
     **/
    @RequestMapping(value = "/delPromotion",method = RequestMethod.POST)
    @ResponseBody
    public String delPromotion(int id){
        Map<String,Object> jsonMap = new HashMap();
        int result = activityInfoService.delPromotion(id);
        if(result > 0){
            jsonMap.put("code",1);
            jsonMap.put("msg","删除成功");
        }else{
            jsonMap.put("code",0);
            jsonMap.put("msg","删除失败");
        }
        return JSONObject.toJSONString(jsonMap);
    }

     /*
     * @Author James
     * @Description 添加活动
     * @Date 15:37 2019/2/28
     * @Param
     * @return
     **/
    @RequestMapping(value = "/addPromotion",method = RequestMethod.POST)
    @ResponseBody
    public String addPromotion(@RequestBody ActivityInfo parameter){
        Map<String,Object> jsonMap = new HashMap();
        int result = activityInfoService.addPromotion(parameter);
        if(result > 0){
            jsonMap.put("code",1);
            jsonMap.put("msg","添加成功");
        }else{
            jsonMap.put("code",0);
            jsonMap.put("msg","添加失败");
        }
        return JSONObject.toJSONString(jsonMap);
    }

    /*
     * @Author James
     * @Description 更新活动
     * @Date 15:37 2019/2/28
     * @Param
     * @return
     **/
    @RequestMapping(value = "/updatePromotion",method = RequestMethod.POST)
    @ResponseBody
    public String updatePromotion(@RequestBody ActivityInfo parameter){
        Map<String,Object> jsonMap = new HashMap();
        int result = activityInfoService.updatePromotion(parameter);
        if(result > 0){
            jsonMap.put("code",1);
            jsonMap.put("msg","更新成功");
        }else{
            jsonMap.put("code",0);
            jsonMap.put("msg","更新失败");
        }
        return JSONObject.toJSONString(jsonMap);
    }
}
